package main

import (
	"log"
	"os"
	"text/template"
	"time"
)

type User struct {
	Id        int
	Name      string
	Title     string
	CreatedAt time.Time
}

type Class struct {
	Name     string
	Total    int
	Students []User
}

func main() {
	const templa = `班级: {{.Name}}  学生 {{.Total}} 总数:
{{range .Students}}----------------------------------------
Id: {{.Id}}
Name:   {{.Name}}
Title:  {{.Title | printf "%.64s"}}
Age:    {{.CreatedAt | daysAgo}} days
{{end}}`

	var report = template.Must(template.New("issuelist").
		Funcs(template.FuncMap{"daysAgo": daysAgo}).
		Parse(templa))

	students := []User{
		{
			Id:        1,
			Name:      "张三",
			Title:     "班长",
			CreatedAt: time.Date(2001, 1, 2, 0, 0, 0, 0, time.Local),
		},
		{
			Id:        2,
			Name:      "李四",
			Title:     "学习委员",
			CreatedAt: time.Date(2000, 11, 25, 0, 0, 0, 0, time.Local),
		},
		{
			Id:        3,
			Name:      "王五",
			Title:     "组长",
			CreatedAt: time.Date(2000, 8, 16, 0, 0, 0, 0, time.Local),
		},
	}

	result := Class{
		Name:     "三年级一班",
		Total:    len(students),
		Students: students,
	}

	if err := report.Execute(os.Stdout, result); err != nil {
		log.Fatal(err)
	}

}

func daysAgo(t time.Time) string {
	return t.Format("2006-01-02")
}
